package com.hrms.doctor.controller;

import com.hrms.patient.core.controller.BaseController;
import com.hrms.patient.core.domain.AjaxResult;
import com.hrms.patient.core.domain.entity.SysUser;
import com.hrms.patient.core.domain.model.RegisterBody;
import com.hrms.patient.core.page.TableDataInfo;
import com.hrms.doctor.domain.Doctor;
import com.hrms.doctor.domain.DoctorPatient;
import com.hrms.doctor.domain.vo.DoctorPatientVo;
import com.hrms.doctor.domain.vo.DoctorVo;
import com.hrms.doctor.service.DoctorService;
import com.hrms.framework.web.service.SysRegisterService;
import com.hrms.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;



@RestController
@Api(tags = "医生接口")

public class DoctorController extends BaseController {
    @Autowired
    SysRegisterService registerService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    ISysUserService sysUserService;
    //获得信息

    @ApiOperation("查询所有医生")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/doctor")
    public TableDataInfo list( String name, String phone){
        startPage();
        List<Doctor> list = doctorService.selectDoctor( name, phone);
        return getDataTable(list);
    }
    //增加
    @ApiOperation("增加医生")
    @PreAuthorize("@ss.hasRole('admin')")
    @PostMapping("/doctor")
    @Transactional
    public AjaxResult insertDoctor( @Validated Doctor doctor){

        RegisterBody registerBody=new RegisterBody();
        registerBody.setUsername(doctor.getJobId());
        registerBody.setPassword(doctor.getPassword());
        String msg= registerService.registerDoctor(registerBody);
        if(!msg.equals("")){
            return AjaxResult.error(msg);
        }
        SysUser sysUser=sysUserService.selectUserByUserName(doctor.getJobId());
        Long []roleids=new Long[1];
        roleids[0]=100L;
        sysUserService.insertUserAuth(sysUser.getUserId(),roleids);
        doctor.setId(sysUser.getUserId());
        Integer rs=doctorService.insertDoctor(doctor);
        return toAjax(rs);

    }
    //批量删除
    @ApiOperation("批量删除")
    @PreAuthorize("@ss.hasRole('admin')")
    @DeleteMapping("/doctor")
    public AjaxResult deleteById(@RequestParam List<Long> list){
        return toAjax(doctorService.deleteById(list));
    }
    //内容回显
    @ApiOperation("回显医生信息")
    @PreAuthorize("@ss.hasRole('admin')")
    @GetMapping("/doctor/{id}")
    public AjaxResult selectDoctorById(@PathVariable Long id){
        Doctor doctor=doctorService.selectDoctorById(id);
        return success(doctor);
    }

    //修改信息
    @ApiOperation("修改医生")
    @PreAuthorize("@ss.hasRole('admin')")
    @PutMapping("/doctor")
    public AjaxResult update(Doctor doctor){

        return toAjax(doctorService.update(doctor));
    }



    @ApiOperation("查找科室医生")
    @GetMapping("/user/doctor")
    public AjaxResult getDeptDoctor(String name,String deptName){
        List<DoctorVo> list=doctorService.selectDoctorByName(name,deptName);

        return success(list);
    }









    //增加患者
    @ApiOperation("增加患者")
    @PostMapping ("/doctor/patient")
    public AjaxResult insertPatient(@Validated @RequestBody DoctorPatient doctorPatient){

        return toAjax(doctorService.insertPatient(doctorPatient));
    }
    //查询患者
    @ApiOperation("查询患者")
    @GetMapping("/doctor/patient/select/{id}")
    public AjaxResult selectById(String name,String phone, @PathVariable Long id){
        //TODO: 获取token
        List<DoctorPatientVo> list=doctorService.selectDoctorPatient(id,name, phone);
        return success(list);
    }
    //删除患者
    @ApiOperation("批量删除患者")
    @DeleteMapping("/doctor/patient/{id}")
    public AjaxResult deletePatientById(@PathVariable Long id,@RequestParam List<Long> list){
        //TODO: 获取token
        return toAjax(doctorService.deletePatientById(id,list));
    }




}
